<template>
  <el-dialog
    title="审核物业公司"
    :model-value="visible"
    width="500px"
    @close="handleClose"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      v-loading="loading"
    >
      <el-form-item label="审核结果" prop="auditStatus">
        <el-radio-group v-model="form.auditStatus">
          <el-radio :value="1">审核通过</el-radio>
<el-radio :value="2">审核拒绝</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="审核备注" prop="auditRemark">
        <el-input
          v-model="form.auditRemark"
          type="textarea"
          placeholder="请输入审核备注"
          :rows="4"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitLoading">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
/**
 * 物业公司审核组件
 * @author Wu.Liang
 * @date 2025-01-29
 */

import { ref, reactive, watch, nextTick } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import propertyCompanyApi from '@/api/property/propertyCompany'
import type { PropertyCompanyAuditData } from '@/types/property'
import { showSuccess, showError } from '@/utils/message'

// Props
interface Props {
  visible: boolean
  companyId: number
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  companyId: 0
})

// Emits
const emit = defineEmits<{
  success: []
  cancel: []
}>()

// 响应式数据
const formRef = ref<FormInstance>()
const loading = ref(false)
const submitLoading = ref(false)

// 表单数据
const form = reactive<PropertyCompanyAuditData>({
  auditStatus: 1,
  auditRemark: ''
})

// 表单验证规则
const rules: FormRules = {
  auditStatus: [
    { required: true, message: '请选择审核结果', trigger: 'change' }
  ],
  auditRemark: [
    { max: 500, message: '审核备注长度不能超过500个字符', trigger: 'blur' }
  ]
}

// 监听对话框显示状态
watch(
  () => props.visible,
  (visible) => {
    if (visible) {
      nextTick(() => {
        formRef.value?.clearValidate()
        // 重置表单数据
        form.auditStatus = 1
        form.auditRemark = ''
      })
    }
  }
)

// 提交审核
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitLoading.value = true
    
    const response = await propertyCompanyApi.auditPropertyCompany(props.companyId, form)
    
    if (response.code === 200) {
      showSuccess('审核成功')
      emit('success')
    } else {
      showError(response.message || '审核失败')
    }
  } catch (error) {
    console.error('审核失败:', error)
    showError('审核失败')
  } finally {
    submitLoading.value = false
  }
}

// 关闭对话框
const handleClose = () => {
  emit('cancel')
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
</style> 